package com.huang.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huang.eduservice.client.OrderClient;
import com.huang.eduservice.entity.EduCourse;
import com.huang.eduservice.entity.chapter.ChapterVo;
import com.huang.eduservice.entity.frontVo.CourseFrontVo;
import com.huang.eduservice.entity.vo.CourseWebVo;
import com.huang.eduservice.service.EduChapterService;
import com.huang.eduservice.service.EduCourseService;
import com.huang.servicebase.execptionhandler.huangException;
import com.hunag.utils.JwtUtils;
import com.hunag.utils.R;
import com.hunag.utils.orderVo.CourseWebVoOrder;
import com.hunag.utils.orderVo.EduCourseOrder;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/eduservice/courseFront")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("getPageCondition/{page}/{limit}")
    public R getPageCondition(@PathVariable long page,
                              @PathVariable long limit,
                              @RequestBody(required = false) CourseFrontVo courseFrontVo
    ) {
        Page<EduCourse> pageParam = new Page<>(page, limit);
        Map<String, Object> map = courseService.getCourseFrontList(pageParam, courseFrontVo);
        return R.ok().data(map);
    }

    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        CourseWebVo courseWebVo = courseService.getFrontCourseInfo(courseId);

        List<ChapterVo> chapterVideo = chapterService.getChapterVideo(courseId);

        String id = JwtUtils.getMemberIdByJwtToken(request);
        if (Strings.isBlank(id)) {
            throw new huangException(20001, "请登录。");
        }
        //查询redis  是否收藏了
        final String KEY_LIKE = id.substring(5, 8);
        System.out.println(KEY_LIKE);
        Boolean isLike = false;
        Set<CourseWebVo> set = redisTemplate.opsForSet().members(KEY_LIKE);
        for (CourseWebVo o : set) {
            if ((o.getId()).equals(courseId)) {
                isLike = true;
                break;
            }
        }
        boolean buy = orderClient.isBuy(id, courseId);

        return R.ok().data("chapterVideo", chapterVideo).data("courseWebVo", courseWebVo).data("isLike", isLike).data("isbuy", buy);
    }

    //根据课程id查询课程信息
    @PostMapping("getCourseInfoOrder/{courseId}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String courseId, HttpServletRequest request) {
        CourseWebVo courseInfo = courseService.getFrontCourseInfo(courseId);
        CourseWebVoOrder voOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo, voOrder);

        return voOrder;
    }

    @PostMapping("tolike")
    public R tolike(@RequestBody CourseWebVo courseWebVo, HttpServletRequest request) {

        try {
            Boolean flag = false;
            String id = JwtUtils.getMemberIdByJwtToken(request);
            if (Strings.isBlank(id)) {
                throw new huangException(20001, "请登录。");
            }
            final String KEY_LIKE = id.substring(5, 8);
            Set<CourseWebVo> set = redisTemplate.opsForSet().members(KEY_LIKE);
            for (CourseWebVo o : set) {
                if ((o.getId()).equals(courseWebVo.getId())) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                redisTemplate.opsForSet().remove(KEY_LIKE, courseWebVo);
            } else {
                redisTemplate.opsForSet().add(KEY_LIKE, courseWebVo);
            }
            return R.ok().data("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error().data("flag", false);
    }

    @GetMapping("/getMemberLike")
    public R getLike(HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        if (Strings.isBlank(id)) {
            throw new huangException(20001, "请登录。");
        }
        final String KEY_LIKE = id.substring(5, 8);
        Set<CourseWebVo> set = redisTemplate.opsForSet().members(KEY_LIKE);
        return R.ok().data("LikeList", set);
    }

    // 获取购买课程
    @PostMapping("/getBuyCourseList")
    public List<EduCourseOrder> getBuyCourseList(@RequestBody List<String> list){
        return courseService.getBuyCourseList(list);
    }

}
